from IMG2PDF import img2pdf, clear_dir
from SWF2IMG import get_all_img_from_swf
import time
import os, sys
import datetime

dir = os.path.dirname(__file__)
sys.path.append(dir)

if __name__ == '__main__':
    # 执行文件路径
    extract_path = r'.\swfextract.exe'  # 此处一定要用反斜杠
    # swf文件路径
    swf_file_dir = r'./swf/'
    pdf_file_dir = r'./pdf/'
    img_dir = r'./img/'

    # 日志文件名 -----------------------------------
    log_file_name = r'./log/{}.txt'.format(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S'))
    # log_file_name = r'./log/log.txt'
    # ---------------------------------------

    for idx, name in enumerate(os.listdir(swf_file_dir)):
        print('当前进度： {} - {} --------------'.format(idx, name))

        swf_file_name = '{}{}'.format(swf_file_dir, name)

        try:
            # swf转图片
            get_all_img_from_swf(extract_path, swf_file_name, img_dir)

            time.sleep(1)

            # 图片转pdf
            pdf_file_name = '{}{}'.format(pdf_file_dir, name)
            tar_pdf_path = pdf_file_name.replace('.swf', '.pdf')

            img2pdf(tar_pdf_path, img_dir)
        except Exception as e:
            print(e)
            with open(file=log_file_name, mode='a', encoding='utf-8') as f:
                f.write('{}\t{}\n'.format(swf_file_name, e))

        clear_dir(img_dir)

